from django.db import models

class Biders(models.Model):
    bider = models.CharField(max_length=50)
    bidDate = models.DateTimeField()
    bidItemID= models.IntegerField(blank=False)

    def __str__(self):              # __unicode__ on Python 2
        return self.bider

    @classmethod
    def getByBider(cls, bid):
        return cls.objects.get(bider=bid)

    @classmethod
    def exists(cls, BID):
        return len(cls.objects.filter(bider = BID)) > 0

    class Meta:
        ordering = ('bidDate',)

class AuctionData(models.Model):
    user = models.CharField(max_length=50)
    title = models.CharField(max_length = 50)
    category = models.CharField(max_length = 30)
    cPrice = models.DecimalField(max_digits=10, decimal_places=2)
    iDesc = models.TextField(max_length=1000, blank=True)
    sDate = models.DateTimeField()
    eDate = models.DateTimeField()
    aOwner = models.CharField(max_length=30)
    version = models.IntegerField(default=0)
    status = models.CharField(max_length=10)
    bider = models.ManyToManyField(Biders, blank=True)

    def __unicode__(self):
        return self.title

    @classmethod
    def getById(cls, ID):
        return cls.objects.get(id=ID)

    @classmethod
    def getByUser(cls, User):
        return cls.objects.get(user=User)

    @classmethod
    def getByTitle(cls, title):
        return cls.objects.get(title = title)

    @classmethod
    def getByCategory(cls, category):
        return cls.objects.get(category = category)

    @classmethod
    def existByTitle(cls, title):
        return len(cls.objects.filter(title = title)) > 0

    @classmethod
    def exists(cls, ID):
        return len(cls.objects.filter(id=ID)) > 0